package main

import (
	"fmt"
	"github.com/robfig/cron/v3"
	"github.com/shadow1ng/fscan/Plugins"
	"github.com/shadow1ng/fscan/common"
	"log"
	"os"
	"strings"
)

func main() {
	var Info common.HostInfo
	common.Flag(&Info)
	cfgYml, err := common.LoadConfigYml(common.AppConfigEnv.DirPath)
	if err != nil {
		println(err)
		return
	}
	fmt.Printf("===================最大协程数： '%v' ========================\n", common.Threads)
	if strings.Compare("run", common.StartSelect) == 0 {
		Plugins.MainScan(*cfgYml, Info)
	} else {
		fmt.Printf("===================定时任务CRON '%s' ========================\n", cfgYml.Cron)

		secondParser := cron.NewParser(cron.Second | cron.Minute |
			cron.Hour | cron.Dom | cron.Month | cron.DowOptional | cron.Descriptor)
		cron2 := cron.New(cron.WithParser(secondParser), cron.WithChain(cron.SkipIfStillRunning(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags)))))

		_, err = cron2.AddFunc(cfgYml.Cron, func() {
			Plugins.MainScan(*cfgYml, Info)
		})
		if err != nil {
			fmt.Println("错误：", err)
		}
		cron2.Start()
		defer cron2.Stop()
	}
	//最后执行关闭管道
	//defer Plugins.CloseResult()
	//启动/关闭

	select {
	//查询语句，保持程序运行，在这里等同于for{}
	}
}
